 package br.com.ads.sgse.managedBean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.ads.dao.DAO;
import br.com.ads.dao.JPAUtil;
import br.com.ads.sgse.converter.DateConverter;
import br.com.ads.sgse.entity.Professor;
//import br.com.ads.sgse.entity.UnidadeFederacao;

@ManagedBean(name="professorBean")
@ViewScoped
public class ProfessorBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private Professor professor = new Professor();
	private List<Professor> professores;
	private DAO<Professor> dao = new DAO<Professor>(Professor.class);
	//private UnidadeFederacao unidadefederacao = new  UnidadeFederacao();
	private EntityManager em = new JPAUtil().getEntityManager();
	private DateConverter dataConversao;
	private String parameter;
	
	public String getParameter() {return parameter;}
	public void setParameter(String parameter) {this.parameter = parameter;}
	
	/** ACTIONS **/

	@SuppressWarnings("unused")
	@PostConstruct
	private void init() {
		dataConversao = new DateConverter();
		getAllProfessores();
	}

	public String salvar() {
		try {
			professor.setDataNascimento(dataConversao.converter());
			
			if (professor.getId() == null) {
				dao.adiciona(professor);
			} else {
				dao.atualiza(professor);
			}
			getAllProfessores();
			professor = new Professor();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void remove() {
		try {
			dao.remove(professor);
			getAllProfessores();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getAllProfessores() {
		try {
			professores = dao.listaTodos();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getProfessorByNumero() {
		try {
			Query q = em
					.createQuery("SELECT nf FROM Professor nf WHERE nf.numero=:numero");
			q.setParameter("numero", new Long(parameter));
			professor = (Professor) q.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
		
	
	/** getters and setters **/
	


	public Professor getProfessor() {return professor;}
	public void setProfessor(Professor professor) {this.professor = professor;}

	public List<Professor> getProfessores() {return professores;}
	public void setProfessores(List<Professor> professores) {this.professores = professores;}



	public DateConverter getDataConversao() {return dataConversao;}
	public void setDataConversao(DateConverter dataConversao) {this.dataConversao = dataConversao;}



	
	
	
	
	
	
	

}
